//
//  MCZhuihaoTableViewCell.m
//  TLYL
//
//  Created by miaocai on 2017/6/16.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCZhuihaoTableViewCell.h"

@interface MCZhuihaoTableViewCell()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *checkBoxBtn;

@property (weak, nonatomic) IBOutlet UILabel *qihaoLabel;

@property (weak, nonatomic) IBOutlet UITextField *beiShuTextField;

@property (weak, nonatomic) IBOutlet UILabel *jinELabel;

@property (weak, nonatomic) NSString *lastCountStr;

@end

@implementation MCZhuihaoTableViewCell

- (IBAction)checkBoxClick:(UIButton *)sender {
    
    self.checkBoxBtn.selected = !sender.selected;
    self.dataSource.selected = self.checkBoxBtn.selected;
    
    if (self.textFiledDidSelected != nil) {
        self.dataSource.beishu = self.beiShuTextField.text;
        self.textFiledDidSelected(self.dataSource,self.lastCountStr);
    }
    
}



- (void)awakeFromNib {

    [super awakeFromNib];
    self.qihaoLabel.textColor = RGB(119, 119, 119);
    self.beiShuTextField.layer.cornerRadius = 5;
    self.beiShuTextField.layer.borderWidth = 0.5f;
    self.beiShuTextField.layer.borderColor = RGB(119, 119, 119).CGColor;
    self.beiShuTextField.placeholder = @"1";
    [self.checkBoxBtn setTitleColor:RGB(119, 119, 119) forState:UIControlStateNormal];
    [self.checkBoxBtn setTitleColor:RGB(119, 119, 119) forState:UIControlStateSelected];
    self.beiShuTextField.delegate = self;

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];

   
}

- (void)setHighlighted:(BOOL)highlighted{

}


- (void)setDataSource:(MCZhuihaoModel *)dataSource{
    _dataSource = dataSource;
    self.checkBoxBtn.selected = dataSource.selected;
    [self.checkBoxBtn setTitle:dataSource.title forState:UIControlStateNormal];
    [self.checkBoxBtn setTitle:dataSource.title forState:UIControlStateSelected];

    if (self.textFiledDidSelected != nil) {
      
        self.beiShuTextField.text = dataSource.beishu;
        self.jinELabel.text = [NSString stringWithFormat:@"%d",[dataSource.beishu intValue] *2];
        if ([dataSource.beishu isEqualToString:@"0"]||[dataSource.beishu isEqualToString:@""]||dataSource.beishu == nil) {
            self.checkBoxBtn.selected = NO;
            self.dataSource.selected = NO;
        }
        self.textFiledDidSelected(self.dataSource,self.lastCountStr);
    }
  
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.checkBoxBtn.selected = YES;
    self.dataSource.selected = YES;
    if (self.textFiledDidBecomeFisrtR != nil) {
        self.textFiledDidBecomeFisrtR(textField.text, self.frame);
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text  isEqual: @"0"]) {
        self.checkBoxBtn.selected = NO;
        self.dataSource.selected = NO;
    } else {
        self.checkBoxBtn.selected = YES;
        self.dataSource.selected = YES;
    }

    if ([textField.text intValue] >= 9999) {
        textField.text = @"9999";
    }
    if (self.textFiledDidSelected != nil) {
        self.dataSource.beishu = textField.text;
        self.textFiledDidSelected(self.dataSource,self.lastCountStr);
    }
     self.jinELabel.text = [NSString stringWithFormat:@"%d",[self.beiShuTextField.text intValue] *2];
     self.lastCountStr = textField.text;
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];

}

#pragma mark - getter and setter 

- (NSString *)lastCountStr{

    return _lastCountStr;
}
@end
